package com.wb.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wb.reggie.common.R;
import com.wb.reggie.dto.SetmealDto;
import com.wb.reggie.entity.Category;
import com.wb.reggie.entity.Setmeal;
import com.wb.reggie.service.CategoryService;
import com.wb.reggie.service.SetmealDishService;
import com.wb.reggie.service.SetmealService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 套餐管理
 */
@Slf4j
@RestController
@RequestMapping("/setmeal")
public class SetmealController {

    @Autowired
    private SetmealService setmealService;

    @Autowired
    private SetmealDishService setmealDishService;

    @Autowired
    private CategoryService categoryService;

    /**
     * 新增菜品
     * @param setmealDto
     * @return
     */
    @PostMapping
    public R<String> save(@RequestBody SetmealDto setmealDto){
        log.info("新增菜品：{}",setmealDto.toString());
        setmealService.saveWithDish(setmealDto);
        return R.success("新增套餐成功");
    }


    /**
     * 套餐分页查询
     * @param page
     * @param pageSize
     * @param name
     * @return
     */
    @GetMapping("/page")
    public R<Page> page(int page,int pageSize,String name){
        //分页构造器
        Page<Setmeal> pageInfo = new Page<>();
        Page<SetmealDto> setmealDtoInfo = new Page<>();

        //构造条件查询
        LambdaQueryWrapper<Setmeal> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(name != null,Setmeal::getName,name);
        queryWrapper.orderByDesc(Setmeal::getUpdateTime);
        setmealService.page(pageInfo,queryWrapper);

        //拷贝  将pageInfo的内容拷贝到dishDtoInfo中，并忽略pageInfo中的records
        //忽略是因为要把records中的categoryId改成categoryName响应给前端，具体原因看博客第14章
        BeanUtils.copyProperties(pageInfo,setmealDtoInfo,"records");
        List<Setmeal> records = pageInfo.getRecords();//对records内容改造
        List<SetmealDto> list = records.stream().map((item) -> {//item是Dish
            SetmealDto setmealDto = new SetmealDto();
            BeanUtils.copyProperties(item,setmealDto);//将item的属性拷贝到dishDto中
            Long categoryId =  item.getCategoryId();//分类id
            Category category = categoryService.getById(categoryId);//通过categoryId查到分类的数据
            if(category != null){
                String categoryName = category.getName();//拿到分类名称
                setmealDto.setCategoryName(categoryName);//赋值给dishDto
            }
            return setmealDto;
        }).collect(Collectors.toList());
        setmealDtoInfo.setRecords(list);
        return R.success(setmealDtoInfo);

    }

    /**
     * 套餐删除
     * @param ids
     */
    @DeleteMapping
    public R<String> delete(@RequestParam List<Long> ids){
        log.info("ids:{}",ids.toString());
        setmealService.removeWithDish(ids);
        return R.success("套餐数据删除成功");
    }

    /**
     * 根据条件查询套餐信息
     * 注意，如果请求后加的不是json数据，是键值对的话，就不要添加@RequestBody注解
     * 例如请求：http://localhost:8080/setmeal/list?categoryId=1413342269393674242&status=1
     * @param setmeal
     * @return
     */
    @GetMapping("/list")
    public R<List<Setmeal>> list(Setmeal setmeal){
        log.info("套餐信息查询：{}",setmeal.toString());
        LambdaQueryWrapper<Setmeal> queryWrapper = new LambdaQueryWrapper<>();

        queryWrapper.eq(setmeal.getCategoryId() != null,Setmeal::getCategoryId,setmeal.getCategoryId());
        queryWrapper.eq(setmeal.getStatus() != null,Setmeal::getStatus,setmeal.getStatus());

        //排序
        queryWrapper.orderByDesc(Setmeal::getUpdateTime);

        List<Setmeal> list = setmealService.list(queryWrapper);

        return R.success(list);
    }
}
